﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;

namespace SUNRPA.CONTROLS.IMPL.controls {
    public class ClickImgNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            ClickImgNParam? param = item.ControlParam as ClickImgNParam;
            string imgPath = param.ImagePath;
            Bitmap currentDeskTop = ImageUtil.TakeScreenShoot();
            Bitmap searchPic = new Bitmap(imgPath);
            Point tp = ImageUtil.GetPicturePos(currentDeskTop, searchPic, 1);
            if(tp.X == -1 || tp.Y == -1) { 
                throw new RpaRuntimeException("没有找到对应图片");
            }
            //获得图片中心坐标
            Point center = ImageUtil.ReCalculatePos(tp, new Point(0,0), searchPic.Width, searchPic.Height);
            //将鼠标移动至目标位置
            MouseMoveNAction mva = new MouseMoveNAction();
            mva.moveWithTail(center.X, center.Y);
            if(param.ButtonType == 0) {
                //左键
                switch(param.ClickType) {
                    //单击
                    case 0 : 
                        User32Utils.LeftClick();
                        break;
                    //双击
                    case 1 :
                        User32Utils.LeftDoubleClick();
                        break;
                }
            } else { 
                //右键
                switch(param.ClickType) {
                    //单机
                    case 0 : 
                        User32Utils.RightClick();
                        break;
                    //双击
                    case 1 :
                        User32Utils.RightDoubleClick();
                        break;
                }
            }
            return currentIndex;
        }
    }
}
